package com.dog.modules.upms.web;

import cn.hutool.core.lang.UUID;
import com.dog.framework.core.dto.RobotBasicParamReqDTO;
import com.dog.framework.core.utils.JsonUtils;
import com.dog.framework.email.domain.MailSendEntity;
import com.dog.framework.excel.utils.EasyExcelUtil;
import com.dog.framework.redis.service.RedisService;
import com.dog.modules.upms.dto.request.MailSendReqDto;
import com.dog.modules.upms.dto.request.RedisReqDto;
import com.dog.modules.upms.dto.response.ConfigExcelVO;
import com.dog.modules.upms.dto.response.RedisRespDto;
import com.dog.modules.upms.service.SendService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisService redisService;


    @Autowired
    private SendService sendService;


    @Autowired
    private AsyncTaskExecutor asyncTaskExecutor;

    @GetMapping("/hello")
    public Boolean hello() {
        System.out.println("===============hello world upms service===========");
        return Boolean.TRUE;
    }

    @GetMapping("/redis/{name}")
    public String redis(@PathVariable String name) {
        log.info("参数：{}", JsonUtils.toJsonString(name));
        String key = "redis:string:100:";
        redisService.set(key, name);
        String value = (String) redisService.get(key);
        String result = key.concat(value);
        return result;
    }

    @PostMapping("/redis/object")
    public RedisRespDto redisObject(@RequestBody RedisReqDto redisReqDto) {
        log.info("参数：{}", JsonUtils.toJsonString(redisReqDto));
        String key = "redis:object:100:";
        redisService.set(key, JsonUtils.toJsonString(redisReqDto));
        Object value = redisService.get(key);
        RedisReqDto respDto = JsonUtils.parseObject(value.toString(), RedisReqDto.class);
        RedisRespDto result = new RedisRespDto();
        BeanUtils.copyProperties(respDto, result);
        log.info("返回结果：{}", JsonUtils.toJsonString(result));
        return result;
    }


    @PostMapping("/send/email")
    public Boolean sendEmail(@RequestBody MailSendReqDto reqDto) {
        log.info("参数：{}", JsonUtils.toJsonString(reqDto));
        MailSendEntity mailSendEntity = new MailSendEntity();
        BeanUtils.copyProperties(reqDto, mailSendEntity);
        sendService.sendMail(mailSendEntity);
        log.info("返回结果：{}", JsonUtils.toJsonString(Boolean.TRUE));
        return Boolean.TRUE;
    }

    @PostMapping("/send/rabbit")
    public Boolean sendRbbit(@RequestBody RobotBasicParamReqDTO reqDto) {
        log.info("参数：{}", JsonUtils.toJsonString(reqDto));
        reqDto.setRequestId(UUID.randomUUID().toString());
        sendService.sendRabbitMq(reqDto);
        return Boolean.TRUE;
    }



    @SneakyThrows
    @PostMapping("/excel/export")
    public void excelExport(HttpServletResponse response) {

        List<ConfigExcelVO> configExcelVOS = new ArrayList<>();

        ConfigExcelVO bean1 = ConfigExcelVO.builder()
                .id(1L)
                .group("asd")
                .remark("hello")
                .value("world")
                .build();
        ConfigExcelVO bean2 = ConfigExcelVO.builder()
                .id(2L)
                .group("asd")
                .remark("hello")
                .value("world")
                .build();
        ConfigExcelVO bean3 = ConfigExcelVO.builder()
                .id(3L)
                .group("asd")
                .remark("hello")
                .value("world")
                .build();

        configExcelVOS.add(bean1);
        configExcelVOS.add(bean2);
        configExcelVOS.add(bean3);

        // 输出
        EasyExcelUtil.exprotExcel(response, configExcelVOS, "导出测试", "sheet单1", ConfigExcelVO.class);

    }


    @SneakyThrows
    @GetMapping("/async/task/{id}")
    public Integer asyncTask(@PathVariable String id) {
        log.info("参数：{}", JsonUtils.toJsonString(id));

        //获取主线程的请求信息
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();

        //1. 异步获取文章作者的信息
        CompletableFuture<Integer> futureSysUsers = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 100;
        }, asyncTaskExecutor);

        CompletableFuture<Integer> futureCommentTotal = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 200;
        }, asyncTaskExecutor);


        CompletableFuture.allOf(futureSysUsers, futureCommentTotal).join();

        Integer tolal = futureSysUsers.get() + futureCommentTotal.get();

        log.info("返回结果：{}", JsonUtils.toJsonString(tolal));
        return tolal;
    }


}
